package com.p2p.base.util.mail;

import java.util.HashMap;
import java.util.Map;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

/**
 * leiwen@yiqihao.com
 */
public class SendCloudMail {

	public static String API_USER;
	public static String API_KEY;
	public static String URL;
	public static String FROM;

	private static Logger logger = Logger.getLogger(SendCloudMail.class);

	public static void send(String to, String subject, String html) {
		if (!check()) {
			return;
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("api_user", API_USER);
		queryMap.put("api_key", API_KEY);
		queryMap.put("from", FROM);
		queryMap.put("to", to);
		queryMap.put("subject", subject);
		queryMap.put("html", html.substring(0,html.length()));
		HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
				.form(queryMap).send();
		String body = response.bodyText();
		try {
			JSONObject jsonObject = JSONObject.parseObject(body);
			MailMessage msg = JSONObject.toJavaObject(jsonObject,
					MailMessage.class);

			String sendInfo = "to=" + to + ",subject=" + subject;
			if (msg.getMessage().equals(MailMessage.ERROR)) {
				logger.error("sendcloud,send mail failed:" + msg + ",sendInfo:"
						+ sendInfo);
			} else if (msg.getMessage().equals(MailMessage.SUCCESS)) {
				logger.info("sendcloud,send mail ok,sendInfo:" + sendInfo);
			}
		} catch (Exception e) {
			logger.error("send mail failed",e);
			logger.info(body);
		}

		// System.out.println(response);
	}
	public static void send2(String to, String subject, String html) {
		if (!check()) {
			return;
		}
		Map<String, String> queryMap = new HashMap<String, String>();
		queryMap.put("api_user", API_USER);
		queryMap.put("api_key", API_KEY);
		queryMap.put("from", FROM);
		queryMap.put("to", to);
		queryMap.put("subject", subject);
		queryMap.put("html", html.substring(0,html.length()));
		HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
				.query(queryMap).send();
		String body = response.bodyText();
		try {
			JSONObject jsonObject = JSONObject.parseObject(body);
			MailMessage msg = JSONObject.toJavaObject(jsonObject,
					MailMessage.class);

			String sendInfo = "to=" + to + ",subject=" + subject;
			if (msg.getMessage().equals(MailMessage.ERROR)) {
				logger.error("sendcloud,send mail failed:" + msg + ",sendInfo:"
						+ sendInfo);
			} else if (msg.getMessage().equals(MailMessage.SUCCESS)) {
				logger.info("sendcloud,send mail ok,sendInfo:" + sendInfo);
			}
		} catch (Exception e) {
			logger.error("send mail failed",e);
			logger.info(body);
		}

		// System.out.println(response);
	}
	
	private static boolean check() {
		if (StringUtils.isEmpty(API_USER) || StringUtils.isEmpty(API_KEY)
				|| StringUtils.isEmpty(FROM) || StringUtils.isEmpty(URL)) {
			logger.error("sendcloud mail is not configured when send mail!");
			return false;
		}
		return true;

	}

	public static void main(String[] args) {
		testInit();
		String to = "240370818@qq.com";
		String subject = "投标协议";
		String html = "你太棒了！你已成功的从SendCloud发送了一封测试邮件，接下来快登录前台去完善账户信息吧！";
		 send(to, subject, html);

		//String subject2 = "来自SendCloud的第一封邮件！";
		//String html2 = "发送失败";
		//send(to, subject2, html2);
	}

	public static void testInit() {
		API_USER = "whptp_test_J6wu6k";
		API_KEY = "DEZQHP9GhfSOxXju";
		URL = "https://sendcloud.sohu.com/webapi/mail.send.json";
		FROM = "master@whptp.com";
	}

	public static void init(Map<String, String> ctxPropertiesMap) {
		String apiUser = MapUtils.getString(ctxPropertiesMap, "mail.api_user");
		String apiKey = MapUtils.getString(ctxPropertiesMap, "mail.api_key");
		String url = MapUtils.getString(ctxPropertiesMap, "mail.url");
		String from = MapUtils.getString(ctxPropertiesMap, "mail.from");
		if (StringUtils.isEmpty(apiUser)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "apiUser is empty");
		} else {
			API_USER = apiUser;
		}
		if (StringUtils.isEmpty(apiKey)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "apiKey is empty");
		} else {
			API_KEY = apiKey;
		}
		if (StringUtils.isEmpty(url)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "url is empty");
		} else {
			URL = url;
		}
		if (StringUtils.isEmpty(from)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "from is empty");
		} else {
			FROM = from;
		}

	}

}
